<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="search" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          default-expand-all="true"
          :props="defaultProps"
          :expand-on-click-node="false"
          highlight-current
          @current-change="handCurrtChange"
        />
      </div>

      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" :loading="loading" @click="$router.push('/employee/detail')">添加员工</el-button>

          <el-button size="mini" @click="showDialog=true">excel导入</el-button>

          <el-button size="mini" :loading="loading" @click="exportEmployee">excel导出</el-button>

        </el-row>

        <!-- 表格组件 -->
        <!-- 分页 -->
        <el-table :data="tableData">
          <el-table-column align="center" label="头像">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :size="large " :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username.charAt(2) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              <span>{{ row.formOfEmployment===1?'正式':'非正式' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>

              <el-button size="mini" type="text">角色</el-button>

              <el-button size="mini" type="text">删除</el-button>

            </template>

          </el-table-column>

        </el-table>

        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page="page"
            @current-change="handlePageChange"
          />
        </el-row>

      </div>
      <!-- <ImportExcel v-model="showDialog" /> -->
      <ImportExcel :value="showDialog" @input="showDialog=$event" @refresh="getEmployeeList" />
    </div>

  </div>

</template>

<script>
import { getDepartmentListApi } from '@/api/department'
import { transListToTreeData } from '@/utils'
import { getEmployeeList, exportEmployee } from '@/api/employee'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel'

export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      showDialog: false,
      loading: false,
      currtId: null,
      depts: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      tableData: [],
      total: 0,
      page: 1,
      keyword: ''
    }
  },
  created() {
    this.getDepts()
  },
  methods: {
  // 导出员工exportEmployee
    async exportEmployee() {
      try {
        this.loading = true
        const res = await exportEmployee()
        console.log(res)
        FileSaver.saveAs(res, '员工信息表.xlsx')
      } catch (err) {
        this.$message.error('导出失败')
      } finally {
        this.loading = false
      }
    }, // 获取部门列表
    async getDepts() {
      const res = await getDepartmentListApi()
      this.depts = transListToTreeData(res, 0)
      // 默认出现是传值教育的id
      this.currtId = this.depts[0].id
      // 等DOM更新完成以后进行渲染
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.currtId)
      })
      // 进入页面旧获取到数据
      this.getEmployeeList()
    },
    // 点击哪个哪个旧高亮的id存储
    handCurrtChange(node) {
      console.log(node)
      this.currtId = node.id
      // 点击获取数据
      this.getEmployeeList()
      // 重置页码
      this.page = 1
    },
    // 封装获取员工列表
    async getEmployeeList() {
      const res = await getEmployeeList({
        page: this.page,
        pagesize: 8,
        departmentId: 1,
        keyword: this.keyword
      })
      this.tableData = res.rows
      this.total = res.total
    },
    // 获取分页的数据
    handlePageChange(val) {
      this.page = val
      this.getEmployeeList()
    },
    // 搜索获取的值
    search() {
      // 防抖动
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        // 切换到第一页
        this.page = 1
        this.getEmployeeList()
      }, 500)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
